<template>
	<view>
		<view class="editinfo-page flex">
			<view class="avatar-wrap flex">
				<u-avatar :src="userInfo.avatar" shape="circle" size="170"></u-avatar>
				<view class="avatar-wrap__tips">
					<button size="mini" type="default" open-type="chooseAvatar" @chooseavatar="chooseavatar">上传</button>
				</view>
			</view>
			
			<view class="nickname-wrap flex">
				<view class="nickname-wrap__tips flex">
					<u-text text="用户昵称" color="#999" :size="28"></u-text>
				</view>
				<view class="nickname-wrap__input flex">
					<input type="nickname" v-model="userInfo.nickname" placeholder="请输入您的昵称"  @change="getNickname" />
				</view>
			</view>
			
			<view class="nickname-wrap flex">
				<view class="nickname-wrap__tips">
					<u-text text="手机号码" color="#999" :size="28"></u-text>
				</view>
				<view class="nickname-wrap__input">
					<input type="tel" :disabled="true" v-model="userInfo.mobile" placeholder="请输入手机号码" />
					<view>
						<button size="mini" type="primary" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">授权获取</button>
					</view>
				</view>
			</view>
			
			<template v-if="fromType=='login'">
				<view class="nickname-wrap flex">
					<view class="nickname-wrap__tips">
						<u-text text="真实姓名" color="#999" :size="28"></u-text>
					</view>
					<view class="nickname-wrap__input flex">
						<input type="text" v-model="idcheck.realname" placeholder="请输入您的真实姓名" />
					</view>
				</view>
				
				<view class="nickname-wrap flex">
					<view class="nickname-wrap__tips">
						<u-text text="身份证号" color="#999" :size="28"></u-text>
					</view>
					<view class="nickname-wrap__input flex">
						<input type="text" v-model="idcheck.idnumber" placeholder="请输入您的身份证号" />
					</view>
				</view>
				
				<view class="nickname-wrap flex">
					<view class="nickname-wrap__tips">
						<u-text text="联系地址" color="#999" :size="28"></u-text>
					</view>
					<view class="nickname-wrap__input flex">
						<input type="text" v-model="idcheck.address" placeholder="请输入您的联系地址" />
					</view>
				</view>
			</template>
			
			<template v-if="fromType=='edit'">
				<view class="nickname-wrap flex">
					<view class="nickname-wrap__tips">
						<u-text text="个人生日" color="#999" :size="28"></u-text>
					</view>
					<view class="nickname-wrap__input">
						<picker mode="date" :value="userInfo.birthday" @change="bindDateChange">
							<view class="uni-input">{{userInfo.birthday}}</view>
						</picker>
					</view>
				</view>
				
				<view class="nickname-wrap flex">
					<view class="nickname-wrap__tips">
						<u-text text="出游喜好" color="#999" :size="28"></u-text>
					</view>
					<view class="nickname-wrap__input">
						<input type="text" v-model="userInfo.tour_perfer" placeholder="请输入出游喜好" />
					</view>
				</view>
				
				<view class="nickname-wrap flex">
					<view class="nickname-wrap__tips">
						<u-text text="意向景点" color="#999" :size="28"></u-text>
					</view>
					<view class="nickname-wrap__input">
						<input type="text" v-model="userInfo.tour_target" placeholder="请输入意向景点" />
					</view>
				</view>
			</template>
			
			<view class="info-save" @click="confirmSave()">确定保存</view>
		</view>
	</view>
</template>

<script>
	import { wechatPhoneNumber,saveUserInfo,fileUpload } from '@/api/api.js'
	
	export default {
		data() {
			return {
				userInfo: uni.getStorageSync('ftxUserInfo') || {},
				updateAvatar: false,
				fromType: 'edit',
				idcheck: {
					realname: '',
					idnumber: '',
					address: ''
				}
			}
		},
		onLoad(options) {
			this.fromType = options.fromtype || 'edit'
		},
		methods: {
			chooseavatar: function(res) {
				this.userInfo.avatar = res.detail.avatarUrl
				this.updateAvatar = true
			},
			getNickname(e){
				this.userInfo.nickname = uni.$lv.string.trim(e.detail.value)
			},
			bindDateChange: function(e) {
				this.userInfo.birthday = e.detail.value
			},
			decryptPhoneNumber: function(res) {
				wechatPhoneNumber({code:res.detail.code}).then(res => {
					this.userInfo.mobile = res.data.phoneNumber
				})
			},
			confirmSave: async function() {
				var _this = this
				let _postData = _this.userInfo
				
				if ( _this.fromType == 'login' ) {
					if ( !_this.idcheck.realname ) {
						return uni.showToast({
						    title: '请填写真实姓名',
						    icon: 'none'
						})
					}
					
					if ( !_this.idcheck.idnumber ) {
						return uni.showToast({
						    title: '请填写身份证号',
						    icon: 'none'
						})
					}
					
					_postData.realname = _this.idcheck.realname
					_postData.idnumber = _this.idcheck.idnumber
					_postData.address  = _this.idcheck.address
				}
				
				if ( !_this.userInfo.nickname ) {
					return uni.showToast({
					    title: '请完善用户昵称哦~',
					    icon: 'none'
					})
				}
				if ( !_this.userInfo.mobile ) {
					return uni.showToast({
					    title: '请完善手机号码哦~',
					    icon: 'none'
					})
				}
				
				if ( _this.updateAvatar == true ) {
					uni.showLoading({
						title: '头像上传中'
					})
					await fileUpload(_this.userInfo.avatar).then(res => {
						_this.updateAvatar = false
						_this.userInfo.avatar = res.data.url
					})
				}
				
				uni.showLoading({
					title: '保存中'
				})
				
				saveUserInfo(_this.fromType,_postData).then( res => {
					uni.setStorageSync('ftxUserInfo' , res.data)
					uni.setStorageSync('ftxUserId' , res.data.uid)
					uni.hideLoading()
					uni.showToast({
						title: '保存成功~',
						icon: 'none',
						duration: 2000,
						success() {
							if ( _this.fromType == 'login' ) {
								uni.switchTab({
									url: '/pages/my/my'
								})
							}
							
							if ( _this.fromType == 'edit' ) {
								setTimeout(function(){
									uni.navigateBack()
								},2000)
							}
						}
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.editinfo-page {
	margin-top: 40rpx;
	align-items: center;
	flex-direction: column;
	.avatar-wrap {
		flex-direction: column;
		align-items: center;
		justify-content: center;
		margin-bottom: 80rpx;
		&__tips {
			margin-top: 8rpx;
		}
	}
	.nickname-wrap {
		width: 90%;
		justify-content: center;
		margin-bottom: 20rpx;
		&__tips {
			width: 140rpx;
			@include flex;
			align-items: center;
		}
		&__input {
			@include flex;
			flex: 1;
			border-bottom: 1rpx solid $uni-text-color-disable;
			padding-bottom: 12rpx;
			color: $uni-text-color-placeholder;
			font-size: 28rpx;
			justify-content: space-between;
			align-items: center;
		}
	}
	
	.info-save {
		@include flex;
		box-shadow: 0upx 5upx 10upx rgba(0, 0, 0, 0.2);
		width: 90%;
		height: 80upx;
		text-align: center;
		border-radius: 80upx;
		background-color: #f23a3a;
		color: #fff;
		justify-content: center;
		align-items: center;
		font-size: 30upx;
		margin-top: 40upx;
	}
}
</style>
